//
//  MineViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/23.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "MineViewController.h"

#import "LoginViewController.h"
#import "SetViewController.h"
#import "MyOrderViewController.h"
#import "PersonInfoViewController.h"
#import "YueViewController.h"
#import "MyHouseViewController.h"
#import "MessViewController.h"

#import "SQRZViewController.h"
#import "QDJFViewController.h"
#import "SCViewController.h"
#import "XYDJViewController.h"
#import "MYHBViewController.h"
#import "SXJFViewController.h"
#import "MineCell.h"
@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSArray *array1;
    NSArray *array2;
    NSArray *array1Tits;
    NSMutableArray *array1Dets;
    NSMutableDictionary *dic;
}

@property (nonatomic,strong)UITableView *tableView;
@property (strong, nonatomic) YDDUser *yUser;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    dic = [NSMutableDictionary dictionary];
    array1 = @[@"余额-(2)",@"红包-(3)",@"签到-(2)1",@"积分",@"等级-(1)"];
    array1Tits = @[@"我的余额",@"我的红包",@"签到积分",@"授信积分",@"信用等级"];
    array1Dets = [NSMutableArray array];
    
    [self loadNav];
    [self loadUI];
    
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self preferredStatusBarStyle];
    if (![YDDAPIs share].token.length) {
        [self presentViewController:[LoginViewController new] animated:YES completion:nil];
    }
    [self loadData];
}

- (void)loadNav {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    view.backgroundColor = COLOR_RGB(54, 59, 61, 1);
    view.userInteractionEnabled = YES;
    [self.view addSubview:view];
    
    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"我的";
    lab.textColor = [UIColor whiteColor];
    lab.font = [UIFont systemFontOfSize:18.0];
    [view addSubview:lab];
    [lab makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.centerX);
        make.top.equalTo(34);
    }];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 setImage:[UIImage imageNamed:@"设置"] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(click1) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn1];
    [btn1 makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.left.equalTo(view.left).offset(0);
        make.width.height.equalTo(40);
    }];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setImage:[UIImage imageNamed:@"消息"] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(click2) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn2];
    [btn2 makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.right.equalTo(view.right);
        make.width.equalTo(40);
        make.height.equalTo(38);
    }];
}

- (void)click1 {
    SetViewController *set = [[SetViewController alloc] init];
    [self.navigationController pushViewController:set animated:YES];
}

- (void)click2 {
    MessViewController *mess = [[MessViewController alloc] init];
    [self.navigationController pushViewController:mess animated:YES];
}

- (void)loadUI {
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-49) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;//隐藏滚动条
    [self.view addSubview:_tableView];
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 164)];
    headView.backgroundColor = COLOR_RGB(54, 59, 61, 1);
    _tableView.tableHeaderView = headView;
    
    UIImageView *btn = [[UIImageView alloc] init];
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"avatar"]];
    [btn sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"01_看图王"]];
    btn.layer.cornerRadius = 40.0;
    btn.layer.masksToBounds = YES;
    [headView addSubview:btn];
    [btn makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(80);
        make.top.equalTo(headView.top).offset(8);
        make.centerX.equalTo(headView.centerX);
    }];
    
    UILabel *labName = [[UILabel alloc] init];
    labName.text = [dic objectForKey:@"nickname"];
    labName.font = [UIFont systemFontOfSize:14.0];
    labName.textColor = [UIColor whiteColor];
    [headView addSubview:labName];
    [labName makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(btn.centerX);
        make.top.equalTo(btn.bottom).offset(10);
    }];
    
    UILabel *labRZ = [[UILabel alloc] init];
    labRZ.text = @"未实名";
    labRZ.font = [UIFont systemFontOfSize:14.0];
    labRZ.layer.borderWidth = 1.0;
    labRZ.layer.borderColor = [UIColor colorWithRed:3/255.0 green:191/255.0 blue:222/255.0 alpha:1.0].CGColor;
    labRZ.layer.cornerRadius = 5.0;
    labRZ.textAlignment = NSTextAlignmentCenter;
    labRZ.textColor = [UIColor colorWithRed:3/255.0 green:191/255.0 blue:222/255.0 alpha:1.0];
    [headView addSubview:labRZ];
    [labRZ makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(labName.centerY);
        make.left.equalTo(labName.right).offset(5);
        make.width.equalTo(50.0);
    }];
    
    UILabel *labNum = [[UILabel alloc] init];
    labNum.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"phone"] ];
    labNum.font = [UIFont systemFontOfSize:14.0];
    labNum.textColor = [UIColor whiteColor];
    [headView addSubview:labNum];
    [labNum makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(btn.centerX);
        make.top.equalTo(labName.bottom).offset(10);
    }];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 6;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return array1.count;
    }else{
        return 1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    MineCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MineCell" owner:nil options:nil] lastObject];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.detailTextLabel.font = [UIFont systemFontOfSize:15.0];
    if (indexPath.section == 0) {
        NSString *imgs = array1[indexPath.row];
        
        cell.imageHead.image = [UIImage imageNamed:imgs];
        cell.labName.text = array1Tits[indexPath.row];
        cell.labName.font = [UIFont systemFontOfSize:15.0];
        if (array1Dets.count!=0) {
            cell.labDet.text = array1Dets[indexPath.row];
        }
        
    }else if (indexPath.section == 1){
        
        cell.imageHead.image = [UIImage imageNamed:@"个人资料"];
        cell.labName.text = @"个人资料";
        cell.labName.font = [UIFont systemFontOfSize:15.0];
        NSString *realname = [dic objectForKey:@"realname"];
        if (realname.length == 0) {
            cell.labDet.text = @"未实名";
        }else{
            cell.labDet.text = @"已认证";
        }
        
    }else if (indexPath.section == 2){
        
        cell.imageHead.image = [UIImage imageNamed:@"收藏"];
        cell.labName.text = @"我的收藏";
        cell.labName.font = [UIFont systemFontOfSize:15.0];
        cell.labDet.hidden = YES;
    }else if (indexPath.section == 3){
        cell.imageHead.image = [UIImage imageNamed:@"订单-(1)"];
        cell.labName.text = @"订单明细";
        cell.labName.font = [UIFont systemFontOfSize:15.0];
        cell.labDet.hidden = YES;
    }else if (indexPath.section == 4){
        cell.imageHead.image = [UIImage imageNamed:@"租房"];
        cell.labName.text = @"我的出租房";
        cell.labName.font = [UIFont systemFontOfSize:15.0];
        cell.labDet.hidden = YES;
    }else{
        cell.imageHead.image = [UIImage imageNamed:@"入驻"];
        cell.labName.text = @" 申请入驻";
        cell.labName.font = [UIFont systemFontOfSize:15.0];
        cell.labDet.hidden = YES;
    }
    
    return cell;
}
//cell点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            YueViewController *yue = [[YueViewController alloc] init];
            NSString *pr = [NSString stringWithFormat:@"%.2f",[[dic objectForKey:@"balance"] floatValue]];
            yue.price = pr;
            [self.navigationController pushViewController:yue animated:YES];
        }else if (indexPath.row == 1){
            MYHBViewController *myhb = [[MYHBViewController alloc] init];
            [self.navigationController pushViewController:myhb animated:YES];
        }else if (indexPath.row == 2){
            QDJFViewController *qdjf = [[QDJFViewController alloc] init];
            [self.navigationController pushViewController:qdjf animated:YES];
        }else if (indexPath.row == 3){
            SXJFViewController *sxjf = [[SXJFViewController alloc] init];
            [self.navigationController pushViewController:sxjf animated:YES];
        }else{
            XYDJViewController *xydj = [[XYDJViewController alloc] init];
            NSString *xy = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"creditrating"] integerValue]];
            xydj.jf = xy;
            [self.navigationController pushViewController:xydj animated:YES];
        }
        
    }else if (indexPath.section == 1){
        PersonInfoViewController *info = [[PersonInfoViewController alloc] init];
        NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"avatar"]];
        info.imageUrl = imgUrl;
        info.pickName = [dic objectForKey:@"nickname"];
        
        [self.navigationController pushViewController:info animated:YES];
    }else if (indexPath.section == 2){
        SCViewController *sc = [[SCViewController alloc] init];
        [self.navigationController pushViewController:sc animated:YES];
    }else if (indexPath.section == 3){
        MyOrderViewController *myorder = [[MyOrderViewController alloc] init];
        [self.navigationController pushViewController:myorder animated:YES];
    }else if (indexPath.section == 4){
        MyHouseViewController *house = [[MyHouseViewController alloc] init];
        [self.navigationController pushViewController:house animated:YES];
    }else{
        SQRZViewController *sjrz = [[SQRZViewController alloc] init];
        [self.navigationController pushViewController:sjrz animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 10.0;
    }else{
        return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

//我的页面数据
- (void)loadData {
    
    [YDDAPIs userCenterDataSuccess:^(YDDUser *yUser, NSString *errMsg) {
        if (!errMsg) {
            YLog(@"%@", yUser);
            self.yUser = yUser;
        } else {
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    } failure:^(NSError *error) {
        YLog(@"%@", error);
    }];
    
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:token forKey:@"verifyToken"];
    NSString *userid = [NSString stringWithFormat:@"%li",[[user objectForKey:@"userid"] integerValue]] ;
    if (userid.length != 0) {
        [parmas setObject:userid forKey:@"userid"];//int
    }
    
    NSLog(@"parmas:%@",parmas);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/index",urlHead];
    [manager POST:url parameters:parmas progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSLog(@"我的res:%@",responseObject);
        
        NSString *status = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"status"] integerValue]];
        if ([status integerValue] == 200) {
            dic = [responseObject objectForKey:@"content"];
            NSString *yue = [NSString stringWithFormat:@"%.2f元",[[dic objectForKey:@"balance"] floatValue]];
            NSString *honbao = [NSString stringWithFormat:@"%.2f元",[[dic objectForKey:@"redenvelope"] floatValue]];
            NSString *qdjf = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"points"] integerValue]];
            NSString *sxjf = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"creditscore"] integerValue]];
            NSString *xydj = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"creditrating"] integerValue]];
            [array1Dets addObject:yue];
            [array1Dets addObject:honbao];
            [array1Dets addObject:qdjf];
            [array1Dets addObject:sxjf];
            [array1Dets addObject:xydj];
        }
        
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}



@end
